define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'booking/services/index' + location.search,
                    add_url: 'booking/services/add',
                    edit_url: 'booking/services/edit',
                    del_url: 'booking/services/del',
                    multi_url: 'booking/services/multi',
                    import_url: 'booking/services/import',
                    table: 'booking_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'store.name',
                            title: __('Store_id'),
                            formatter: Controller.api.content,
                            operate: false
                        },
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'category_names', title: __('Category_ids'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'stocks', title: __('Stocks')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'market_price', title: __('Market_price'), operate:'BETWEEN'},
                        {field: 'views', title: __('Views')},
                        {field: 'sales', title: __('Sales')},
                        {field: 'status', title: __('Status'), searchList: {"up":__('Status up'),"down":__('Status down'),"audit":__('Status audit')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            
            $("#c-category_ids").data("params", function (obj) {
                //{"custom[types]":"1","isTree":true}
                return {"isTree":true,"custom[types]":"3","custom[store_id]":$("#c-store_id").val()};
            });
            
            $(document).on("change", "#c-store_id", function(){
                //后续操作
                $('#c-category_ids').selectPageClear();
                $("#c-category_ids").data("params", function (obj) {
                    return {"isTree":true,"custom[types]":"3","custom[store_id]":$("#c-store_id").val()};
                });
            });
            Controller.api.bindevent();
        },
        edit: function () {
            
            $("#c-category_ids").data("params", function (obj) {
                //{"custom[types]":"1","isTree":true}
                return {"isTree":true,"custom[types]":"3","custom[store_id]":$("#c-store_id").val()};
            });
            
            $(document).on("change", "#c-store_id", function(){
                //后续操作
                $('#c-category_ids').selectPageClear();
                $("#c-category_ids").data("params", function (obj) {
                    return {"isTree":true,"custom[types]":"3","custom[store_id]":$("#c-store_id").val()};
                });
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
